package com.cskaoyan.service;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.CommentListVo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommentServiceImpl implements CommentService {
    @SneakyThrows
    @Override
    public BaseRespVo list(Integer page, Integer limit, String userIdStr, String valueIdStr, String sort, String order) {
        PageHelper.startPage(page, limit);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCommentMapper commentMapper = sqlSession.getMapper(MarketCommentMapper.class);

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria marketCommentExampleCriteria = marketCommentExample.createCriteria();
        marketCommentExampleCriteria.andDeletedEqualTo(false);

        if (userIdStr!=null&&userIdStr!=""){
            if (canParseToInt(userIdStr)){
                int userId = Integer.parseInt(userIdStr);
                marketCommentExampleCriteria.andUserIdEqualTo(userId);
            }else{
                // 进入这里表示输入的不是数字，添加一个矛盾的筛选条件，从而让返回一个空的list
                marketCommentExampleCriteria.andDeletedEqualTo(true);
            }
        }
        if (valueIdStr!=null&&valueIdStr!=""){
            if (canParseToInt(valueIdStr)){
                int valueId = Integer.parseInt(valueIdStr);
                marketCommentExampleCriteria.andUserIdEqualTo(valueId);
            }else{
                // 进入这里表示输入的不是数字，添加一个矛盾的筛选条件，从而让返回一个空的list
                marketCommentExampleCriteria.andDeletedEqualTo(true);
            }
        }

        List<MarketComment> marketComments = commentMapper.selectByExample(marketCommentExample);
        List<Map> maps = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        for(MarketComment comment : marketComments){
            Map map = new HashMap<>();
            map.put("addTime",comment.getAddTime());
            map.put("adminContent",comment.getAdminContent());
            map.put("content",comment.getContent());
            map.put("deleted",comment.getDeleted());
            map.put("hasPicture",comment.getHasPicture());
            map.put("id",comment.getId());
            map.put("picUrls",objectMapper.readValue(comment.getPicUrls(),String[].class));
            map.put("star",comment.getStar());
            map.put("type",comment.getType());
            map.put("updateTime",comment.getUpdateTime());
            map.put("userId",comment.getUserId());
            map.put("valueId",comment.getValueId());
            maps.add(map);
        }

        // 来源于MyBatis的查询结果 ，使用PageInfo有参构造方法来封装这个PageInfo → 前面要使用OrderMapper做一个查询
        PageInfo pageInfo = new PageInfo(maps);
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok(CommonData.data(pageInfo));


    }

    @Override
    public BaseRespVo delete(CommentListVo commentListVo) {
        Integer id = commentListVo.getId();

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCommentMapper commentMapper = sqlSession.getMapper(MarketCommentMapper.class);

        MarketComment comment = commentMapper.selectByPrimaryKey(id);
        comment.setDeleted(true);

        commentMapper.updateByPrimaryKey(comment);

        sqlSession.commit();
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok();
    }

    public boolean canParseToInt(String str) {
        for (char c : str.toCharArray()) {
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        try {
            Integer.parseInt(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
